package com.hqyj.service.impl;

import com.hqyj.pojo.User;
import com.hqyj.dao.UserMapper;
import com.hqyj.service.UserService;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author rock
 * @since 2021-10-21
 */
@Service
public class UserServiceImpl implements UserService {
    //注入mapper
    @Autowired(required = false)
    private UserMapper userMapper;

    @Override
    public HashMap<String, String> select(User user) {
        return null;
    }

    @Override
    public HashMap<String, String> login(User user) {
        HashMap<String, String> map = new HashMap<String, String>();

        try {
            //1 用户令牌(用户和密码对象)
            UsernamePasswordToken token = new UsernamePasswordToken(user.getUsername(), user.getPassword());
            //2 创建shiro的用户对象
            Subject su = SecurityUtils.getSubject();
            //3 登录
            su.login(token);
            map.put("info", "登录成功");
        } catch (UnknownAccountException u) {
            u.printStackTrace();
            map.put("info", "用户名不正确");
            System.out.println("用户名不正确");
        } catch (IncorrectCredentialsException e) {
            e.printStackTrace();
            map.put("info", "密码不正确");
            System.out.println("密码不正确");
        }

        return map;
    }
}
